package com.ciaojian.client.policy.impl.coupon;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ciaojian.core.factory.ObjectMapperFactory;
import com.ciaojian.core.mapper.MemberMapper;
import com.ciaojian.core.mapper.MyCouponMapper;
import com.ciaojian.core.model.HaircutService;
import com.ciaojian.core.service.ICouponDiscount;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 会员折扣
 *
 * @author Atlakyn
 */
@Slf4j
@Service
public class MemberCardDiscount implements ICouponDiscount<Map<String, Object>> {
    @Resource
    private MemberMapper dMemberMapper;
    @Resource
    private MyCouponMapper dMyCouponMapper;

    /**
     * 用户会员卡价格
     *
     * @param couponInfo 优惠信息 {userId:用户id,type:服务类型}
     * @param amount     原价格
     * @return 计算后的价格
     */
    @Override
    public BigDecimal discountAmount(Map<String, Object> couponInfo, BigDecimal amount) throws JsonProcessingException {
        // 用户id
        Integer userId = MapUtil.getInt(couponInfo, "userId");

        Map<String, Object> myMember = dMemberMapper.getMyMember(userId);
        if (myMember == null) {
            log.info("用户不是会员[userId:{}]", userId);
            return amount;
        }

        // 服务类型
        String type = MapUtil.getStr(couponInfo, "type");
        // 是否开启全部服务 0否 1是
        Integer allService = MapUtil.getInt(myMember, "allService");

        // 权益校验不通过返回原价
        if (!checkService(allService, type)) {
            return amount;
        }

        // 会员卡优惠折扣
        String memberDiscount = MapUtil.getStr(myMember, "discount");
        ObjectMapper om = ObjectMapperFactory.getObjectMapperInstance();
        Map map = om.readValue(memberDiscount, Map.class);
        // 服务类型
        LambdaQueryWrapper<HaircutService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(HaircutService::getTitle, type);
        HaircutService haircutService = new HaircutService().selectOne(lambdaQueryWrapper);
        Assert.notNull(haircutService, "服务类型有误");
        // 服务名称
        BigDecimal discount = MapUtil.get(map, haircutService.getType().getDescription(),BigDecimal.class);

        // 计算优惠后的价格 amount * (discount / 10)
        BigDecimal divide = discount.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_HALF_UP);
        amount = amount.multiply(divide);
        return amount.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : amount;
    }

    /**
     * 检查会员卡权益服务是否通过
     *
     * @param allService 是否开启全部服务
     * @param type       服务类型
     * @return 通过返回 true
     */
    private boolean checkService(Integer allService, String type) {
        // 是否开启全部服务 0否 1是
        if (allService == 1) {
            return true;
        }
        // type 为空
        if (StrUtil.isEmpty(type)) {
            log.info("checkService未通过,type 为空");
            return false;
        }
        // 会员权益 的服务类型不包含 type
        if (!dMyCouponMapper.getTitles().contains(type)) {
            log.info("checkService未通过,会员权益不支持的服务类型.<dMyCouponMapper.getTitles():{},type:{}>", dMyCouponMapper.getTitles(), type);
            return false;
        }
        return true;
    }
}
